<?
session_start();
include "connect.php";
require_once('recaptchalib.php');
$output = json_encode(array("status"=>"error", "message"=>"nok"));

$username = mysql_real_escape_string($_POST['username']);
$email = mysql_real_escape_string($_POST['email']);
$password = mysql_real_escape_string($_POST['password']);

$validation = md5($email.time());

// Get a key from https://www.google.com/recaptcha/admin/create
$publickey = "6LeiL8kSAAAAAI0FixE2yUBb2pdOLqqb5VX4ue44";
$privatekey = "6LeiL8kSAAAAAKJGWLKAgvsLvfbIi2dHVcNqFBrc";

# the response from reCAPTCHA
$resp = null;
# the error code from reCAPTCHA, if any
$error = null;

# was there a reCAPTCHA response?
if ($_POST["recaptcha_response_field"]) {
	$resp = recaptcha_check_answer ($privatekey,
									$_SERVER["REMOTE_ADDR"],
									$_POST["recaptcha_challenge_field"],
									$_POST["recaptcha_response_field"]);
	if ($resp->is_valid) {				
		$abfrage = "INSERT INTO ffxivc_users (
						userName
						,email
						,password
						,cdate
						,udate
						 )
					VALUES (
						\"".$username."\"
						,\"".$email."\"
						,\"".md5($email.$password)."\"
						,\"".date('Y-m-d H:i:s')."\"
						,\"".date('Y-m-d H:i:s')."\"
						)";

		$res = mysql_query($abfrage);
		$toId = mysql_insert_id();
		if ($res){
			$output = json_encode(array("status"=>"success","message"=>"ok"));
			$abfrage = "INSERT INTO ffxivc_messages (
								parentId
								,fromId
								,toId
								,title
								,message
								,cdate
								,udate
								 )
							VALUES (
								0
								,\"1\"
								,\"".$toId."\"
								,\"Welcome!\"
								,\"Hi!
Thanks for showing interest in this site!
If you have suggestions or bugs you like to report don't hesitate to PM me. 
Cheers
~Nemi\"
								,\"".date('Y-m-d H:i:s')."\"
								,\"".date('Y-m-d H:i:s')."\"
								)";

			$res = mysql_query($abfrage);
			
		}else{
			$output = json_encode(array("status"=>"error", "message"=>mysql_error()));
		}
	}else{
		$output = json_encode(array("status"=>"error", "message"=>"captcha"));
	}
}

echo $output; 

?>